/***************************************************************************************************
 * Copyright (c) 2015-2020 Intelligent Network System Ltd. All Rights Reserved. 
 * 
 * This software is the confidential and proprietary information of Founder. You shall not disclose
 * such Confidential Information and shall use it only in accordance with the terms of the 
 * agreements you entered into with Founder. 
***************************************************************************************************/

#ifndef __SX127X_TIMER_H__
#define __SX127X_TIMER_H__
/***************************************************************************************************
 * INCLUDES
 */
#include "stdint.h"
#include "stdbool.h"

#ifdef __cplusplus
extern "C"{
#endif



/***************************************************************************************************
 * GLOBAL FUNCTIONS DECLEAR
 */
/*!
 * \brief Timer time variable definition
 */
#ifndef TimerTime_t
typedef uint64_t TimerTime_t;
#endif

/*!
 * \brief Timer object description
 */
typedef struct TimerEvent_s
{
    TimerTime_t Timestamp;         //! Current timer value
    TimerTime_t ReloadValue;       //! Timer delay value
    bool IsRunning;             //! Is the timer currently running
    void ( *Callback )( void ); //! Timer IRQ callback function
    struct TimerEvent_s *Next;  //! Pointer to the next Timer object.
    bool IsStart;               //! start or stop in Isr
    struct TimerEvent_s *IsrNext;   // !Pointer to the next Timer Object used in Isr start stop
}TimerEvent_t;


typedef struct TimerHighEvent_s
{
    void (*Callback)( void );
    struct TimerHighEvent_s *Next;
}TimerHighEvent_t;

/*!
 * \brief Enables/Disables low power timers usage
 *
 * \param [IN] enable [true]RTC timer used, [false]Normal timer used
 */
void TimerSetLowPowerEnable( bool enable );

/*!
 * \brief Initializes the timer object
 *
 * \retval enable [true]RTC timer used, [false]Normal timer used
 */
bool TimerGetLowPowerEnable( void );


void sx127x_TimerInit(void);
/*!
 * \brief Initializes the timer object
 *
 * \remark TimerSetValue function must be called before starting the timer.
 *         this function initializes timestamp and reload value at 0.
 *
 * \param [IN] obj          Structure containing the timer object parameters
 * \param [IN] callback     Function callback called at the end of the timeout
 */
void TimerInit( TimerEvent_t *obj, void ( *callback )( void ) );

/*!
 * Timer IRQ event handler
 */
void TimerIrqHandler( void );

/*!
 * \brief Starts and adds the timer object to the list of timer events
 *
 * \param [IN] obj Structure containing the timer object parameters
 */
void TimerStart( TimerEvent_t *obj );
void TimerStartAtTick( TimerEvent_t *obj, TimerTime_t tick );
/*!
 * \brief Stops and removes the timer object from the list of timer events
 *
 * \param [IN] obj Structure containing the timer object parameters
 */
void TimerStop( TimerEvent_t *obj );

/*!
 * \brief Resets the timer object
 *
 * \param [IN] obj Structure containing the timer object parameters
 */
void TimerReset( TimerEvent_t *obj );

/*!
 * \brief Set timer new timeout value
 *
 * \param [IN] obj   Structure containing the timer object parameters
 * \param [IN] value New timer timeout value
 */
void TimerSetValue( TimerEvent_t *obj, TimerTime_t value );

/*!
 * \brief Read the current time
 *
 * \retval time returns current time
 */
TimerTime_t TimerGetTick( void );

/*!
 * \brief Manages the entry into ARM cortex deep-sleep mode
 */
void TimerLowPowerHandler( void );

void TimerStartInIsr( TimerEvent_t *obj );
void TimerStopInIsr( TimerEvent_t *obj );

void TimerAddHighEvent( TimerHighEvent_t *obj );


#ifdef __cplusplus
}
#endif


#endif /* __sx127x_TIMER_H__ */
 
 
 
